package com.group.admin.MqListener;

import com.group.admin.service.ShopCommodityService;
import com.group.common.to.SeckillOrderTo;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author lins
 * @date 2021/11/17 14:10
 */
@Service
@Slf4j
@RabbitListener(queues = "stock.relase.stock.queue")
public class ReleaseStockListener {
    @Autowired
    ShopCommodityService commodityService;
    @RabbitHandler
    public void releaseStock(SeckillOrderTo to, Channel channel, Message message) throws IOException {
        try {
            log.info("解锁库存>>>>>>{}",to);
            commodityService.releaseStock(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }

    }
}
